/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seGobangClient.uiSystem2D;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.vecmath.Point2i;
import seGobangClient.gaming.IPlayer;
import seGobangResources.PathRes;

/**
 *
 * @author Leo
 */
public class GameScene
{

    private MyPanel myPanel;
    private boolean valid = false;
    private MyButton removeB;
    private MyButton removeW;
    private MyButton surrenderB;
    private MyButton surrenderW;
    private MyButton back;
    private JLabel player1;
    private JLabel player2;
    private PathRes path = new PathRes();
    private getpane player1Pane;
    private getpane player2Pane;
    private getpane Pane;

    public void initGameUI(final JFrame container, final getpane mainPane)
    {

        Pane = mainPane;
        container.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        try
        {
            mainPane.imgpath = path.getPath("Model/wf3");
            mainPane.remove(mainPane.getComponentAt(535, 0));
        } catch (Exception e)
        {

        }
        player1Pane = new getpane(151, 120, "");
        player2Pane = new getpane(151, 120, "");
        player1Pane.setLayout(null);
        player2Pane.setLayout(null);
        try
        {
            player1Pane.imgpath = path.getPath("Texture/Gaming/backblackA");
            player2Pane.imgpath = path.getPath("Texture/Gaming/backwhiteA");
        } catch (Exception e)
        {
        }
        player1Pane.setSize(151, 120);
        player2Pane.setSize(151, 120);
        player1Pane.setLocation(446, 45);
        player2Pane.setLocation(446, 229);
        mainPane.add(player1Pane);
        mainPane.add(player2Pane);
        myPanel = new MyPanel();
        myPanel.setLocation(45, 45);
        mainPane.add(myPanel);
        try
        {
            removeB = new MyButton("Texture/Gaming/regretA", "Texture/Gaming/regretB", 64, 30);
            removeW = new MyButton("Texture/Gaming/regretA", "Texture/Gaming/regretB", 64, 30);
            surrenderB = new MyButton("Texture/Gaming/surrenderA", "Texture/Gaming/surrenderB", 64, 30);
            surrenderW = new MyButton("Texture/Gaming/surrenderA", "Texture/Gaming/surrenderB", 64, 30);
            back = new MyButton("Texture/Gaming/abandonA", "Texture/Gaming/abandonB", 75, 30);
        } catch (Exception e)
        {

        }

        removeB.setLocation(9, 87);
        removeB.setSize(64, 30);
        removeW.setSize(64, 30);
        removeW.setLocation(9, 87);
        removeB.setVisible(false);
        removeW.setVisible(false);


        surrenderB.setSize(64, 30);
        surrenderB.setLocation(78, 87);
        surrenderB.setVisible(false);

        surrenderW.setSize(64, 30);
        surrenderW.setLocation(78, 87);
        surrenderW.setVisible(false);


        removeB.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                if (valid)
                {
                    SceneManager.MyCurrentController.doEvent("ChooseRegret", (byte) -1);
                }
            }
        });
        removeW.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                if (valid)
                {
                    SceneManager.MyCurrentController.doEvent("ChooseRegret", (byte) 1);
                }
            }
        });
        surrenderB.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                if (valid)
                {
                    SceneManager.MyCurrentController.doEvent("ChooseSurrender", (byte) -1);
                }
            }
        });
        surrenderW.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                if (valid)
                {
                    SceneManager.MyCurrentController.doEvent("ChooseSurrender", (byte) 1);
                }
            }
        });



        back.setSize(75, 30);
        back.setLocation(550, 410);
        mainPane.add(back);



        player1 = new JLabel();
        player1.setVerticalAlignment(JLabel.NORTH);

        player1.setSize(150, 118);
        player1.setLocation(3, 0);
        player1Pane.add(player1);
        player1.setFont(new Font("宋体", Font.PLAIN, 15));
        player1.setForeground(Color.white);
        player2 = new JLabel();
        player2.setVerticalAlignment(JLabel.NORTH);
        player2.setSize(150, 118);
        player2.setLocation(3, 0);
        player2Pane.add(player2);
        player2.setFont(new Font("宋体", Font.PLAIN, 15));
        player2.setForeground(Color.white);
        player1Pane.add(removeB);
        player1Pane.add(surrenderB);
        player2Pane.add(removeW);
        player2Pane.add(surrenderW);

        back.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                SceneManager.MyCurrentController.doEvent("HaltGame", null);
            }
        });
        container.setVisible(true);
        container.repaint();
    //backMusic.start();*/

    }

    public void addChess(Point2i p, byte flag)
    {
        myPanel.PaddChess(p.x, p.y, flag);
    }

    public void clearChessBoard()
    {
        myPanel.PclearChessBoard();
    }

    public void setValid(boolean valid)
    {
        this.valid = valid;
        myPanel.valid = valid;
    }

    public void updatePlayerInfo(IPlayer blackPlayer, IPlayer whitePlayer)
    {
        if (blackPlayer instanceof seGobangClient.gaming.LocalPlayer)
        {
            if (!removeB.isVisible())
            {
                removeB.setVisible(true);
                surrenderB.setVisible(true);
            }
        }
        if (whitePlayer instanceof seGobangClient.gaming.LocalPlayer)
        {
            if (!removeW.isVisible())
            {
                removeW.setVisible(true);
                surrenderW.setVisible(true);
            }
        }
        player1.setText("<html>" + blackPlayer.getPlayerName() + "<br>第" + blackPlayer.getTurns() + "手<br>悔棋次数：" + blackPlayer.getLastRegrets() + "<br>剩余时间" + blackPlayer.getLastTime()/60+"分"+(blackPlayer.getLastTime()-60*(blackPlayer.getLastTime()/60))+"秒");
        player2.setText("<html>" + whitePlayer.getPlayerName() + "<br>第" + whitePlayer.getTurns() + "手<br>悔棋次数：" + whitePlayer.getLastRegrets() + "<br>剩余时间" + whitePlayer.getLastTime()/60+"分"+(whitePlayer.getLastTime()-60*(whitePlayer.getLastTime()/60))+"秒");
        if (blackPlayer.getLastRegrets() == 0)
        {
            removeB.setEnabled(false);
        } else
        {
            removeB.setEnabled(true);
        }
        if (whitePlayer.getLastRegrets() == 0)
        {
            removeW.setEnabled(false);
        } else
        {
            removeW.setEnabled(true);
        }
    }

    public void setCurrentPlayer(byte ChessFlag)
    {
        if (ChessFlag == -1)
        {
            try
            {
                player2Pane.imgpath = path.getPath("Texture/Gaming/backwhiteA");
                player1Pane.imgpath = path.getPath("Texture/Gaming/backblackB");
                player2Pane.repaint();
                player1Pane.repaint();
            } catch (Exception e)
            {
            }
        } else
        {
            try
            {
                player2Pane.imgpath = path.getPath("Texture/Gaming/backwhiteB");
                player1Pane.imgpath = path.getPath("Texture/Gaming/backblackA");
                player2Pane.repaint();
                player1Pane.repaint();
            } catch (Exception e)
            {
            }
        }
    }

    public void clear()
    {
        try
        {
            Pane.remove(removeB);
            removeB = null;
            Pane.remove(myPanel);
            myPanel = null;
            Pane.remove(player1Pane);
            player1Pane = null;
            Pane.remove(Pane.getComponentAt(535, 0));
            Pane.remove(back);
            back = null;
            Pane.remove(removeW);
            removeW = null;
            Pane.remove(player2Pane);
            player2Pane = null;
            Pane.remove(surrenderB);
            surrenderB = null;
            Pane.remove(surrenderW);
            surrenderW = null;
        } catch (Exception e)
        {

        }
    }
}
